
\setcounter{ipycommand}{0}

\begin{frame}
\frametitle{NumPy}
\reference{
More info: \url{http://www.scipy.org/NumPy_for_Matlab_Users}
}
\overlayicon{Logo/numpy}
\begin{itemize}
  \item Numpy provides a good website for mapping MATLAB syntax into
        equivalent NumPy syntax:\\
        \url{http://www.scipy.org/NumPy_for_Matlab_Users}
  \item This tutorial will only cover the most common cases.
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{NumPy Array}
\overlayicon{Logo/numpy}
\begin{itemize}
  \item Use \lstpy{numpy.array} to represent multidimensional arrays.
\end{itemize}
\begin{lstlisting}
(*@\pyprompt@*) A = array([1, 2, 3])    # create a one dimensional array from a list

(*@\pyprompt@*) B = array([[1, 2, 3],
                 [4, 5, 6]])  # create a two dimensional array from a list of lists

(*@\pyprompt@*) C = array([B, B, B, B]) # create a 3D array from a list of 2D arrays

(*@\pyprompt@*) A.shape         # the size of each dimension
(*@\pyreturn@*) (3,)

(*@\pyprompt@*) B.shape
(*@\pyreturn@*) (2,3)

(*@\pyprompt@*) C.shape
(*@\pyreturn@*) (4,2,3)

(*@\pyprompt@*) A.ndim          # the number of dimensions
(*@\pyreturn@*) 1

(*@\pyprompt@*) A.dtype         # the data type of each element
(*@\pyreturn@*) dtype('int64')

\end{lstlisting}
\end{frame}


\begin{frame}[fragile]
\frametitle{NumPy Array}
\overlayicon{Logo/numpy}
\begin{itemize}
  \item Unlike MATLAB, operations on \lstpy{numpy.array}s are element-wise.
\end{itemize}
\vspace{-2em}
\begin{columns}[t]
\column{0.45\textwidth}
\begin{matlabBlock}
> M = [1 2; 2 1];
> disp(M*M)
   5   4
   4   5

> disp(M.*M)
   1   4
   4   1
\end{matlabBlock}
\column{0.45\textwidth}
\begin{pythonBlock}
> M = array([[1, 2],[2, 1]])
> print M*M
[[1 4]
 [4 1]]

> print dot(M,M)
[[5 4]
 [4 5]]
\end{pythonBlock}
\end{columns}
\begin{itemize}
  \item The function \lstpy{numpy.dot} is required for matrix (or tensor) multiplication.
  \item This syntax may seem ugly, but makes operations more consistent over
        arrays with dimensions greater than 2.
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{NumPy Matrix}
\overlayicon{Logo/numpy}
\reference{
More info: \url{http://www.scipy.org/NumPy_for_Matlab_Users}
}
\begin{itemize}
  \item Numpy provides a special \lstpy{numpy.matrix} subclass of \lstpy{numpy.array}.
  \item \lstpy{numpy.matrix} acts more like a MATLAB matrix.
\end{itemize}
\vspace{-2em}
\begin{columns}[t]
\column{0.45\textwidth}
\begin{matlabBlock}
> M = [1 2; 2 1];
> disp(M*M)
   5   4
   4   5
\end{matlabBlock}
\column{0.45\textwidth}
\begin{pythonBlock}
> M = matrix('1 2; 2 1')
> print M*M
[[5 4]
 [4 5]]
\end{pythonBlock}
\end{columns}
\pause
\begin{itemize}
  \item \lstpy{numpy.matrix} can use a MATLAB-style string initializer.
  \item Use ``\lstpy{from numpy.matlib import *}'' to override common array constructors
        (\lstpy{zeros()}, \lstpy{eye()}, etc.) with matrix versions.
  \pause
  \item \lstpy{numpy.matrix} only handles two dimensional arrays.
  \item \lstpy{numpy.array} is more consistent and still preferred in most cases.
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{NumPy Numerical Data Types}
\overlayicon{Logo/numpy}
\reference{
More info: \url{http://docs.scipy.org/doc/numpy/user/basics.types.html}
}
\begin{itemize}
  \item Numpy adds several numerical data types to Python
\end{itemize}
\footnotesize
\begin{tabular}{|c|p{0.5\linewidth}|}
\hline
\textbf{Names} & \textbf{Descriptions} \\
\hline
\lstpy{bool} & Boolean (\lstpy{True} or \lstpy{False}) stored in a byte \\
\lstpy{int}  & Platform integer (usually \lstpy{int32} or \lstpy{int64}) \\
\lstpy{int8}, \lstpy{int16}, \lstpy{int32}, \lstpy{int64} & 8, 16, 32 and 64 bit signed integers \\
\lstpy{uint8}, \lstpy{uint16}, \lstpy{uint32}, \lstpy{uint64} & 8, 16, 32 and 64 bit unsigned integers \\
\lstpy{float}  &  Shorthand for \lstpy{float64} \\
\lstpy{float16}, \lstpy{float32}, \lstpy{float64}  & half, single, and double precision floats \\
\lstpy{complex}  &  Shorthand for \lstpy{complex128} \\
\lstpy{complex64}, \lstpy{complex128}  & complex numbers with single and double precision floats for real and imaginary parts  \\
\hline
\end{tabular}
\normalsize
\pause
\begin{itemize}
  \item Type can be specified during array creation and converted later
\end{itemize}
\begin{lstlisting}
(*@\pyprompt@*) A = np.array([1, 2, 3], dtype=np.float32)   # construct as type float32

(*@\pyprompt@*) A
(*@\pyreturn@*) array([ 1.,  2.,  3.], dtype=float32)

(*@\pyprompt@*) A.astype('complex')             # convert the type of an existing array
(*@\pyreturn@*) array([ 1.+0.j,  2.+0.j,  3.+0.j])
\end{lstlisting}
\end{frame}


\begin{frame}[fragile]
\frametitle{NumPy Array Construction}
\overlayicon{Logo/numpy}
\reference{
More info: \url{http://www.scipy.org/NumPy_for_Matlab_Users}
}
\begin{itemize}
  \item Common helper functions for constructing arrays are similar to MATLAB.
\end{itemize}
\vspace{-2em}
\begin{columns}[t]
\column{0.45\textwidth}
\begin{matlabBlock}
> M = zeros(3,4);
> M = ones(3,4);
> M = eye(3);
> M = diag([1 2 3]);
> M = diag(A);
> M = rand(3,4);
> M = repmat(A, m, n);
\end{matlabBlock}
\column{0.45\textwidth}
\begin{pythonBlock}
> M = zeros((3,4))
> M = ones((3,4))
> M = eye(3)
> M = diag([1, 2, 3])
> M = diag(A)
> M = random.rand(3,4)
> M = tile(A, (m, n))
\end{pythonBlock}
\end{columns}
\end{frame}


\begin{frame}[fragile]
\frametitle{NumPy Array Slicing}
\overlayicon{Logo/numpy}
\reference{
More info: \url{http://www.scipy.org/NumPy_for_Matlab_Users}
}
\begin{itemize}
  \item Slicing allow access to subset of arrays.
  \item Python slicing syntax is \lstpy{first:end:step}.
\end{itemize}
\footnotesize
\begin{tabular}{|c|c|p{4cm}|}
\hline
\textbf{MATLAB} & \textbf{NumPy} & \textbf{Description} \\
\hline
\lstmat{a(end)} & \lstpy{a[-1]} & access last element in the 1xn matrix \lstpy{a}. \\
\lstmat{a(2,5)} & \lstpy{a[1,4]} & access element in second row, fifth column. \\
\lstmat{a(2,:)} & \lstpy{a[1]} or \lstpy{a[1,:]} & access entire second row of \lstpy{a}. \\
\lstmat{a(1:5,:)} & \lstpy{a[0:5]} or \lstpy{a[:5]} or \lstpy{a[0:5,:]} & access the first five rows of \lstpy{a}. \\
\lstmat{a(3:2:21,:)} & \lstpy{a[ 2:21:2,:]} & every other row of \lstpy{a}, from the third to the twenty-first. \\
\lstmat{a(1:2:end,:)} & \lstpy{a[ ::2,:]} & every other row of \lstpy{a}, starting with the first. \\
\lstmat{a(end:-1:1,:)} & \lstpy{a[ ::-1,:]} & \lstpy{a} with rows in reverse order. \\
\hline
\end{tabular}
\end{frame}


\begin{frame}[fragile]
\frametitle{NumPy Indexing Tricks -- Stacking}
\overlayicon{Logo/numpy}
\reference{
More info: \url{http://www.scipy.org/NumPy_for_Matlab_Users}
}
\begin{itemize}
  \item Numpy provides a special class to enable several indexing tricks.
  \item The \lstpy{r_} class provides row stacking of arrays
  \item \lstpy{r_} is used with brackets as if it is being indexed.
\end{itemize}
\begin{lstlisting}
(*@\pyprompt@*) r_[[1, 2, 3], [4, 5, 6], [7, 8]]   # stack 1D arrays along the first axis
(*@\pyreturn@*) array([1, 2, 3, 4, 5, 6, 7])

(*@\pyprompt@*) r_[eye(2), 2*ones((2,2)) ]         # stack 2D arrays along the first axis
(*@\pyreturn@*)
array([[ 1.,  0.],
       [ 0.,  1.],
       [ 2.,  2.],
       [ 2.,  2.]])
\end{lstlisting}
\begin{itemize}
  \item A variant named \lstpy{c_} is same, but for column stacking.
\end{itemize}
\begin{lstlisting}
(*@\pyprompt@*) c_[eye(2), 2*ones((2,2)) ]         # stack 2D arrays along the second axis
(*@\pyreturn@*)
array([[ 1.,  0.,  2.,  2.],
       [ 0.,  1.,  2.,  2.]])
\end{lstlisting}
\end{frame}


\begin{frame}[fragile]
\frametitle{NumPy Indexing Tricks -- Range Generation}
\overlayicon{Logo/numpy}
\reference{
More info: \url{http://www.scipy.org/NumPy_for_Matlab_Users}
}
\begin{itemize}
  \item The \lstpy{r_} class also provides range generation.
  \item If an argument uses slice notation it is expanded to a range
  \item \lstpy{r_[first:end:step]} is the same as \lstpy{arange(first,end,step)}
\end{itemize}
\begin{lstlisting}
(*@\pyprompt@*) r_[5:10:0.5]     # generates a range from 5 to 10 with step 0.5
(*@\pyreturn@*) array([ 5. ,  5.5,  6. ,  6.5,  7. ,  7.5,  8. ,  8.5,  9. ,  9.5])
\end{lstlisting}
\pause
\begin{itemize}
  \item When the step is replaced with an imaginary number the slice has a different interpretation, \lstpy{first:last:num}
\end{itemize}
\begin{lstlisting}
(*@\pyprompt@*) r_[5:10:5j]     # generates 5 samples between 5 and 10, inclusive
(*@\pyreturn@*) array([  5.  ,   6.25,   7.5 ,   8.75,  10.  ])
\end{lstlisting}
\end{frame}


\begin{frame}[fragile]
\frametitle{NumPy Indexing Tricks}
\overlayicon{Logo/numpy}
\reference{
More info: \url{http://www.scipy.org/NumPy_for_Matlab_Users}
}
\begin{itemize}
  \item The \lstpy{ix_} function generates indexes for irregular slices
\end{itemize}
\footnotesize
\begin{tabular}{|c|c|p{4cm}|}
\hline
\textbf{MATLAB} & \textbf{NumPy} & \textbf{Description} \\
\hline
\lstmat{a([2,4,5],[1,3])} & \lstpy{a[ix_([1,3,4],[0,2])]} & rows 2,4 and 5 and columns 1 and 3. \\
\hline
\end{tabular}
\end{frame}
